package org.gd.auth.impl;

import lombok.extern.slf4j.Slf4j;
import org.gd.auth.entity.Managers;
import org.gd.auth.mapper.ManagersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class SsoUserDetailsService implements UserDetailsService {

    @Autowired
    private ManagersMapper managersMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String nicknme) throws UsernameNotFoundException {
        //1.查询数据库是否有当前用户信息 if true get密码校验 成功之后
        //log.debug("密码为"+passwordEncoder.encode("123123"));
        //DB操作


        String pad = passwordEncoder.encode("123");
        System.out.println("登陆成功 密码为:" + pad);
        System.out.println("nicknme" + nicknme);
        Managers managers = managersMapper.selectByAccount(nicknme);
        System.out.println(managers.getNicknme() + managers.getPwd());
        if (managers == null) {
            System.out.println("用户不存在");
        }
        return new User(nicknme, passwordEncoder.encode(managers.getPwd()), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    }

}
